package com.ssuu.ssuusnow.config.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.io.File;
import java.util.Collections;
import java.util.Scanner;

public class MyGenerator {
    public static void main(String[] args) {

        File directory = new File("");//参数为空
        String courseFile = "D:\\pdf";
//        try {
//            courseFile = directory.getCanonicalPath();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        String url = "jdbc:mysql://localhost:3306/ceshi?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8";
        String username = "root";
        String password = "1234567";
        String author = "sun";
        System.out.println("请输入表名: ");
        Scanner str = new Scanner(System.in);
        String tablePrefix = "su_";
        String tableName = str.next();
        String outputDir = courseFile + "\\src\\main\\java";
        String basePackage = "com.ssuu.ssuusnow.project.manage." + tableName;
        String mapperLocation = courseFile + "\\src\\main\\resources\\mybatis\\manage\\" + tableName;

        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(outputDir)// 指定输出目录
                            .disableOpenDir() // 禁止打开输出目录
                            .dateType(DateType.TIME_PACK) // 指定时间类型
                            .commentDate("yyyy-MM-dd HH:ss:mm"); // 注释日期
                })
                .packageConfig(builder -> {
                    builder.parent(basePackage) // 设置父包名
                            .moduleName("") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, mapperLocation)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tablePrefix + tableName) // 设置需要生成的表名
                            .enableCapitalMode()// 开启大写命名
                            .addTablePrefix(tablePrefix)// 设置过滤表前缀
                            .entityBuilder() // 实体类配置
                            .enableLombok() // 开启lombok注解
                            .enableTableFieldAnnotation() // 开启生成实体时生成字段注解
                    ;
                })
                .templateEngine(new VelocityTemplateEngine()) // 使用默认的Velocity引擎模版
                .execute();
    }
}
